package sa.com.sure.ejtimaa.database;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.model.AttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MeetingAttachments {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MeetingAttachments";
	
    public static final String MeetingIDField = "MeetingID";
    public static final String AttachmentIDField = "AttachmentID";
    
    
    private String[] allColumns = { MeetingIDField,AttachmentIDField};
    
    Cursor cursor = null;
    
    public MeetingAttachments(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static MeetingAttachments Instance = new MeetingAttachments(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public MeetingAttachmentInfo GetItem(int MeetingID,String AttachmentID) throws SQLException
	{	
		try	
		{
			open();
		
			MeetingAttachmentInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingIDField + "=" + MeetingID +" and " + AttachmentIDField + "='" + AttachmentID + "'" ,null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
	
	public List<MeetingAttachmentInfo> GetItemsByMeetingId(int MeetingID) throws SQLException
	{	
		try	
		{
			open();
		
			List<MeetingAttachmentInfo> items = new ArrayList<MeetingAttachmentInfo>(); 
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingIDField + "=" + MeetingID  ,null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		do{
        			items.add(getItemInfo(cursor));	     
        		}
        		while(cursor.moveToNext());
        	
        	return items;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public void addItem(MeetingAttachmentInfo item) throws IOException 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(MeetingIDField, item.GetMeetingID());  
	        values.put(AttachmentIDField, item.GetAttachmentID());
	         
	        SaveFile(item);
	        
	        dbHelper.db.insert(TABLE_Name, null, values); 
	        
	        Attachments.Instance.addItem(item);
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
	
	
	
    private MeetingAttachmentInfo getItemInfo(Cursor cursor) {  
    	
    	MeetingAttachmentInfo itemInfo = new MeetingAttachmentInfo();
    	
    	itemInfo.SetMeetingID(cursor.getInt(cursor.getColumnIndex(MeetingIDField)));
    	itemInfo.SetAttachmentID(cursor.getString(cursor.getColumnIndex(AttachmentIDField)));
    	
    	
    	AttachmentInfo attachment = Attachments.Instance.GetItem(itemInfo.GetAttachmentID());
    	
    	//itemInfo = (MeetingAttachmentInfo) attachment;
    	
    	itemInfo.SetFileName(attachment.GetFileName());
    	itemInfo.SetFileSize(attachment.GetFileSize());
    	itemInfo.SetFileType(attachment.GetFileType());    
    	itemInfo.SetFileContent(attachment.GetFileContent());    	
    	
		return itemInfo;
	}
    
    private boolean SaveFile(MeetingAttachmentInfo item) throws IOException{
    	
    	String path = String.format("/%s/" ,item.GetMeetingID() );
    	Helper.SaveFile(path, item.GetFileName(), item.GetFileContent());
    	
    	return true;
    }
    
    public String GetFilePath(int meetingID){
    	        	
    	String path = String.format("/%s/" ,meetingID );
    
    	return path;
    }
}
